package qf.wlh.com.oneprisdent;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import fragment.ActivityFragment;
import fragment.BranchFragment;
import fragment.MainFragment;
import fragment.RecordFragment;
import fragment.SpeakerFragment;

public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener{

    private DrawerLayout drawerLayout;
    private NavigationView navigationview;
    private RelativeLayout relativeLayout;
    private FrameLayout containFrameLayout;
    private Resources resources;
    private FragmentManager fragmentManager;
    private RelativeLayout search;
    private EditText editText;

    private long exitTime = 0 ;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.container,new MainFragment(),"MianFragment").commit();
        initView();
    }

    private void initView() {
        drawerLayout = ((DrawerLayout) findViewById(R.id.activity_main));
        navigationview = ((NavigationView) findViewById(R.id.main_navigationview));
        navigationview.setItemIconTintList(null);//图片小时本身的颜色
        view = navigationview.getHeaderView(0);

        relativeLayout= (RelativeLayout) view.findViewById(R.id.relativelayout);

        //容器
        containFrameLayout = ((FrameLayout) findViewById(R.id.container));

//        设置监听
        //设置navigation 中menuitem 的选中的颜色
        resources = ((Resources) getBaseContext().getResources());
        ColorStateList colorStateList = resources.getColorStateList(R.color.navigation_menu_item_color);
        navigationview.setItemTextColor(colorStateList);

        search= (RelativeLayout) view.findViewById(R.id.main_search);
        editText= (EditText) view.findViewById(R.id.main_edittext);
        initlister();

    }

    private void initlister() {

        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,LoginActivity.class));
            }
        });
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(MainActivity.this,SearchActivity.class));
            }
        });
        navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //////////////////////////////////////////////////////////////////////
                //设置item可点击，布局中的点击状态图片可以其作用了
                item.setChecked(true);
                drawerLayout.closeDrawer(navigationview);
                switch (item.getItemId()){
                    case R.id.menu_main:
                        fragmentManager.beginTransaction().replace(R.id.container,new MainFragment(),"MainFragment").commit();
                        break;
                    case R.id.menu_speak:
                        startActivity(new Intent(MainActivity.this,SpeakActivity.class));
                        break;
                    case R.id.menu_speaker:
                        fragmentManager.beginTransaction().replace(R.id.container,new SpeakerFragment(),"SpeakerFragment").commit();
                        break;
                    case R.id.menu_branch:
                        fragmentManager.beginTransaction().replace(R.id.container,new BranchFragment(),"BranchFragment").commit();
                        break;
                    case R.id.menu_recoder:
                        fragmentManager.beginTransaction().replace(R.id.container,new RecordFragment(),"RecordFragment").commit();
                        break;
                    case R.id.menu_activity:
                        fragmentManager.beginTransaction().add(R.id.container,new ActivityFragment(),"ActivityFragment").commit();
                        break;
                    case R.id.menu_setting:
                        startActivity(new Intent(MainActivity.this,SettingActivity.class));
                        break;
                }
                return true;
            }
        });

    }

    public void jumpSideMenu(View view) {
      //  navigationview.setVisibility(View.VISIBLE);
      //  navigationview.setVisibility(View.GONE);
        if (!drawerLayout.isDrawerOpen(navigationview)){
            drawerLayout.openDrawer(navigationview);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Snackbar.make(view ,"再按一次退出",Snackbar.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
        finish();
        System.exit(0);
        }
    }

    /**
     * DrawLayout的监听事件
     * @param drawerView
     * @param slideOffset
     */
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(View drawerView) {

    }

    /**
     * 关闭的时候调用
     * @param drawerView
     */
    @Override
    public void onDrawerClosed(View drawerView) {

    }

    /**
     * 状态改变的时候调用
     * @param newState
     */
    @Override
    public void onDrawerStateChanged(int newState) {

    }
}
